/*
 * 文件名：PushParkLot.java	 
 * 时     间：下午7:11:21
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.manager.push;

import java.util.List;

import com.cz073586.message.websocket356.push.PushManager;
import com.cz073586.message.websocket356.transfer.TransferVo;
import com.cz073586.tools.util.Tools;
import com.cz073586.tools.util.UUIDGenerateUtil;
import com.local.dao.mapers.CarParkInMapper;
import com.local.dao.mapers.ParklotNumberMapper;
import com.local.entity.vo.ParklotVo;
import com.local.model.CarParkInExample;
import com.local.model.ParklotNumber;
import com.qianhai.common.entity.PageInfo;
import com.qianhai.common.utils.Log4jUtil;

/**
 * 类描述:推送车位信息,车位总数减去在场车辆
 * 
 * @ClassName: PushParkLot
 * @author Administrator
 * @date 2015年9月29日 下午7:11:21
 */
public class PushParkLotRunble implements Runnable
{
	private static Log4jUtil log = Log4jUtil.getLog4j(PushParkLotRunble.class);
	
	// 进场车辆
	private CarParkInMapper carInMapper;
	
	private ParklotNumberMapper lotNumberMapper;
	
	
	
	public PushParkLotRunble(CarParkInMapper carInMapper,
			ParklotNumberMapper lotNumberMapper)
	{
		super();
		this.carInMapper = carInMapper;
		this.lotNumberMapper = lotNumberMapper;
	}



	@Override
	public void run()
	{
		
		try
		{
			TransferVo<ParklotVo> transCar = new TransferVo<ParklotVo>();
			transCar.setEventType("lotCar");
			
			List<ParklotNumber> listNumber = lotNumberMapper
					.queryByPage(new PageInfo<ParklotNumber>());
			
			int carNumber = 200;
			
			if (Tools.isEmpty(listNumber) || listNumber.isEmpty())
			{
				// 插入默认车位数据
				ParklotNumber tmp = new ParklotNumber();
				tmp.setUuid(UUIDGenerateUtil.generate());
				tmp.setNumber(carNumber);
				lotNumberMapper.insertSelective(tmp);
			}
			
			if (!Tools.isEmpty(listNumber) && !listNumber.isEmpty())
			{
				carNumber = listNumber.get(0).getNumber();
			}
			
			// 统计在场车辆
			
			CarParkInExample ex = new CarParkInExample();
			ex.or().andStatusEqualTo(1);
			int exsitNumber = carInMapper.countByCondition(ex);
			
			ParklotVo lotVo = new ParklotVo();
			lotVo.setCarAllLot(carNumber);
			lotVo.setDayIn(exsitNumber);
			
			int freeNumber = carNumber - exsitNumber;
			lotVo.setFreeLot(freeNumber<0?0:freeNumber);
			
			transCar.setData(lotVo);
			
			PushManager.instance().pushMessage(transCar);
		} catch (Throwable e)
		{
			log.error("", e);
		}
	}
}
